//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.wsd.smartcarwasher.configuration;

import com.wsd.smartcarwasher.domain.Admin;
import com.wsd.smartcarwasher.service.AdminService;
import javax.annotation.Resource;

import com.wsd.smartcarwasher.service.CardOrderService;
import com.wsd.smartcarwasher.service.MenuService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource.Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Set;

public class ShiroRealm extends AuthorizingRealm {
    private static final Logger logger = LoggerFactory.getLogger(AuthorizingRealm.class);
    @Resource
    private AdminService adminService;


    @Resource
    private MenuService menuService;

    public ShiroRealm() {
    }

    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        Admin admin = (Admin)principals.getPrimaryPrincipal();
        Set<String> perms = menuService.listPerms(admin.getId().longValue());
        authorizationInfo.setStringPermissions(perms);
        logger.info("获取用户权限成功[id="+admin.getId()+",name="+admin.getNickname()+"]");
        return authorizationInfo;
    }

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String cellphone = (String)token.getPrincipal();
        Admin admin = this.adminService.getAdminByCellphone(cellphone);
        return admin == null ? null : new SimpleAuthenticationInfo(admin, admin.getPassword(), Util.bytes(admin.getSalt()), this.getClass().getName());
    }
}
